﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Project01
{
   public class AnimationSprite
    {
        public Texture2D Texture { get; private set; }
        public Vector2 Position;
        public Vector2 Velocity;
        public Vector2 Origin;
        public bool Active = true;
        public float Scale = 1;
        public float Rotation;
        public float ZLayer;
        public Color Color = Color.White;
        public SpriteEffects spriteEffects = SpriteEffects.None;
        public int Rows { get; set; }
        public int Columns { get; set; }
        public int totalFrame;
        public int currentFrame;
        public int RecWidth { get { return Texture.Width / Columns; }  }
        public int RecHeight { get { return Texture.Height / Rows; }  } 
        public AnimationSprite(Texture2D texture, int rows, int columns)
        {
            this.Texture = texture;
            this.Rows = rows;
            this.Columns = columns;
            currentFrame = 0;
            totalFrame = Rows * Columns;
        }
        public Rectangle rectangle(Vector2 Position)
        {
            return new Rectangle((int)Position.X, (int)Position.Y, RecWidth, RecHeight);
        }
        public void Update()
        {
            currentFrame++;
            if (currentFrame == totalFrame)
                currentFrame = 0;
        }
        public void Update(GameTime gameTime, int startFrame, int endFrame, float frameSpeed)
        {
            if (currentFrame >= startFrame && currentFrame < endFrame)
            {
            
                    currentFrame++;
                
            }
            else
                currentFrame = startFrame ;

        }
        public void Draw(SpriteBatch spritebatch)
        {
            int width = Texture.Width / Columns;
            int height = Texture.Height / Rows;
            int row = (int)((float)currentFrame / (float)Columns);
            int column = currentFrame % Columns;
            Rectangle sourceRectangle = new Rectangle(width * column, height * row, width, height);
             spritebatch.Draw(Texture, Position, sourceRectangle, Color, Rotation, Origin, Scale, spriteEffects, ZLayer);
        }
    }
}
